package com.bsu.database.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.bsu.bean.Blog;
import com.bsu.bean.User;
import com.bsu.util.HibernateUtil;

public class UserDAO {
	private Logger logger = Logger.getLogger(UserDAO.class);
	@SuppressWarnings("unchecked")
	public List<User> getUser(String login, String password) throws ClassNotFoundException, SQLException {
		List<User> user = new ArrayList<User>();
			

		Session session = HibernateUtil.getSessionfactory().openSession();
			Transaction transaction = null;
			try {
				transaction = session.beginTransaction();
				user = session.getNamedQuery("LOGIN_USER").setParameter(0, login).setParameter(1, password).list();
				transaction.commit();
				
				
			} catch (HibernateException e) {
				transaction.rollback();
				logger.error(e.toString());
				
			} 

		return user;
	}
	
	public boolean add(User user) throws ClassNotFoundException {
		Session session = HibernateUtil.getSessionfactory().openSession();

		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			session.saveOrUpdate(user);
			transaction.commit();
			return true;
		} catch (HibernateException e) {
			transaction.rollback();
			logger.error(e.toString());
		}

		return false;
	}
}
